
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>移动端图片裁剪工具</title>
    <style>
        :root {
            font-size: 16px;
        }
        @media (max-width: 768px) {
            :root {
                font-size: 14px;
            }
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            margin: 0;
            padding: 1rem;
            background: #f5f5f5;
            touch-action: manipulation;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 1rem;
            max-width: 100%;
        }
        .upload-area {
            border: 2px dashed #007AFF;
            border-radius: 12px;
            padding: 2rem;
            text-align: center;
            background: rgba(0, 122, 255, 0.05);
            margin-bottom: 1rem;
            position: relative;
        }
        .controls {
            background: white;
            border-radius: 12px;
            padding: 1rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        .slider-container {
            margin: 1.5rem 0;
        }
        input[type="range"] {
            width: 100%;
            -webkit-appearance: none;
            height: 6px;
            border-radius: 3px;
            background: #e0e0e0;
            outline: none;
        }
        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            width: 24px;
            height: 24px;
            border-radius: 50%;
            background: #007AFF;
            cursor: pointer;
        }
        button {
            background: #007AFF;
            color: white;
            border: none;
            padding: 0.75rem 1.5rem;
            border-radius: 8px;
            font-size: 1rem;
            margin: 0.5rem 0.5rem 0.5rem 0;
            cursor: pointer;
            -webkit-tap-highlight-color: transparent;
        }
        .preview-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 1rem;
            margin-top: 1rem;
        }
        .preview-item {
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .preview-item img {
            width: 100%;
            height: auto;
            display: block;
        }
        @media (max-width: 480px) {
            .preview-container {
                grid-template-columns: 1fr 1fr;
            }
            button {
                padding: 0.75rem 1rem;
                font-size: 0.9rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>图片批量裁剪</h1>
        
        <div class="upload-area" id="dropZone">
            <p>点击或拖放图片到此处</p>
            <input type="file" id="fileInput" accept="image/*" multiple style="display:none">
        </div>
        
        <div class="controls">
            <h2>裁剪设置</h2>
            <div class="slider-container">
                <label>顶部裁剪: <span id="topValue">26%</span></label>
                <input type="range" id="topCrop" min="0" max="50" value="26">
            </div>
            <div class="slider-container">
                <label>底部裁剪: <span id="bottomValue">31%</span></label>
                <input type="range" id="bottomCrop" min="0" max="50" value="31">
            </div>
            <button id="cropBtn">裁剪图片</button>
            <button id="downloadAllBtn">下载全部</button>
        </div>
        
        <div class="preview-container" id="previewContainer"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 移动端优化：增加触控反馈
            const buttons = document.querySelectorAll('button');
            buttons.forEach(btn => {
                btn.addEventListener('touchstart', function() {
                    this.style.opacity = '0.7';
                });
                btn.addEventListener('touchend', function() {
                    this.style.opacity = '1';
                });
            });

            // 其他原有JS代码保持不变（与桌面版相同）
            const fileInput = document.getElementById('fileInput');
            const dropZone = document.getElementById('dropZone');
            const topCrop = document.getElementById('topCrop');
            const bottomCrop = document.getElementById('bottomCrop');
            const topValue = document.getElementById('topValue');
            const bottomValue = document.getElementById('bottomValue');
            const cropBtn = document.getElementById('cropBtn');
            const downloadAllBtn = document.getElementById('downloadAllBtn');
            const previewContainer = document.getElementById('previewContainer');
            
            let uploadedFiles = [];
            let croppedResults = [];
            
             // 事件监听
            dropZone.addEventListener('click', () => fileInput.click());
            fileInput.addEventListener('change', handleFileSelect);
            dropZone.addEventListener('dragover', handleDragOver);
            dropZone.addEventListener('drop', handleDrop);
            topCrop.addEventListener('input', updateCropValues);
            bottomCrop.addEventListener('input', updateCropValues);
            cropBtn.addEventListener('click', processAllImages);
            downloadAllBtn.addEventListener('click', downloadAllImages);
            
            // 更新裁剪比例显示
            function updateCropValues() {
                topValue.textContent = topCrop.value + '%';
                bottomValue.textContent = bottomCrop.value + '%';
            }
            
            // 处理文件选择
            function handleFileSelect(e) {
                if (e.target.files.length > 0) {
                    handleFiles(e.target.files);
                }
            }
            
            // 处理拖放
            function handleDragOver(e) {
                e.preventDefault();
                dropZone.style.borderColor = '#666';
            }
            
            function handleDrop(e) {
                e.preventDefault();
                dropZone.style.borderColor = '#ccc';
                if (e.dataTransfer.files.length > 0) {
                    handleFiles(e.dataTransfer.files);
                }
            }
            
            // 处理上传的文件
            function handleFiles(files) {
                const validFiles = Array.from(files).filter(file => 
                    file.type.match('image/jpeg') || 
                    file.type.match('image/png')
                );
                
                if (validFiles.length === 0) {
                    alert('请选择有效的图片文件(JPG/PNG)');
                    return;
                }
                
                uploadedFiles = [...uploadedFiles, ...validFiles];
                updateFileList();
            }
            
            // 更新文件列表显示
            function updateFileList() {
                fileList.innerHTML = uploadedFiles.length > 0 ? 
                    `<p>已选择 ${uploadedFiles.length} 张图片</p>` : 
                    '<p>未选择图片</p>';
            }
            
            // 处理所有图片裁剪
            function processAllImages() {
                if (uploadedFiles.length === 0) {
                    alert('请先选择图片');
                    return;
                }
                
                previewContainer.innerHTML = '';
                croppedResults = [];
                
                const topPercent = parseInt(topCrop.value) / 100;
                const bottomPercent = parseInt(bottomCrop.value) / 100;
                
                uploadedFiles.forEach((file, index) => {
                    const reader = new FileReader();
                    
                    reader.onload = function(e) {
                        const img = new Image();
                        img.onload = function() {
                            const canvas = document.createElement('canvas');
                            const ctx = canvas.getContext('2d');
                            
                            // 计算裁剪尺寸
                            const topPixels = Math.floor(img.height * topPercent);
                            const bottomPixels = Math.floor(img.height * bottomPercent);
                            const croppedHeight = img.height - topPixels - bottomPixels;
                            
                            // 设置canvas尺寸
                            canvas.width = img.width;
                            canvas.height = croppedHeight;
                            
                            // 执行裁剪
                            ctx.drawImage(
                                img, 
                                0, 
                                topPixels, 
                                img.width, 
                                croppedHeight, 
                                0, 
                                0, 
                                img.width, 
                                croppedHeight
                            );
                            
                            // 获取结果
                            const croppedUrl = canvas.toDataURL('image/jpeg');
                            croppedResults.push({
                                name: 'cropped_' + file.name,
                                url: croppedUrl
                            });
                            
                            // 显示预览
                            displayPreview(croppedUrl, file.name);
                        };
                        img.src = e.target.result;
                    };
                    reader.readAsDataURL(file);
                });
            }
            
            // 显示裁剪结果预览
            function displayPreview(imageUrl, originalName) {
                const previewItem = document.createElement('div');
                previewItem.className = 'preview-item';
                
                const img = document.createElement('img');
                img.src = imageUrl;
                
                const downloadBtn = document.createElement('button');
                downloadBtn.textContent = '下载';
                downloadBtn.onclick = () => downloadImage(imageUrl, 'cropped_' + originalName);
                
                previewItem.appendChild(img);
                previewItem.appendChild(downloadBtn);
                previewContainer.appendChild(previewItem);
            }
            
            // 下载单张图片
            function downloadImage(url, filename) {
                const link = document.createElement('a');
                link.href = url;
                link.download = filename;
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
            
            // 下载所有裁剪结果
            function downloadAllImages() {
                if (croppedResults.length === 0) {
                    alert('请先裁剪图片');
                    return;
                }
                
                croppedResults.forEach(item => {
                    downloadImage(item.url, item.name);
                });
            uploadedFiles = [];
            croppedResults = [];
            }
        });
    </script>
</body>
</html>
